<?php

namespace app\common\model\designermanager;

use think\Model;


class Production extends Model
{

    

    

    // 表名
    protected $name = 'self_designer_production';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'finishtime_text',
        'status_text',
    ];
    
    public function getStatusList()
    {
        return ['0' => "待完成", '1' => "已完成", '2' => "已结算"];
    }
 
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finishtime']) ? $data['finishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setFinishtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getUserInfoAttr($value, $data){
        $value = isset($data['user_id']) ? $data['user_id'] : 0;
        return $value ? \app\common\model\User::getBaseInfoNosense($value) : '';
    }

    public function getNeedsInfoAttr($value, $data){
        $value = isset($data['pringting_id']) ? $data['pringting_id'] : 0;
        if(!$value) return '';

        $res =  \app\common\model\printmanager\Printing::cache(300)->find($value);
        $res->user_info = $res->user_info;
        return $res;
    }

    public function selfdesigner()
    {
        return $this->belongsTo('app\common\model\designermanager\Designer', 'designer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
